package talent.util;

import org.springframework.stereotype.Component;
import talent.entity.Talent;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@Component
public class ScoreCalculator {

    public double calculateCompositeScore(Talent talent) {
        // 创建一个辅助对象用于计算各种分数
        double softwareScore = calculateSoftwareScore(talent.getSoftwareUsageDuration());
        double videoScore = calculateVideoScore(talent.getVideoLearningDuration());
        double planScore = calculatePlanScore(talent.getPlanCompletionRate());
        double timeScore = calculateTimeScore(talent.getUpdatedAt());

        // 综合评分 = 各项分数 × 权重
        return softwareScore * 0.15 +
                videoScore * 0.30 +
                planScore * 0.35 +
                timeScore * 0.20;
    }

    // 软件使用时长分数：越长越好（满分100分）
    private double calculateSoftwareScore(Integer duration) {
        if (duration == null) return 0;
        return Math.min(duration / 30.0, 100);
    }

    // 视频学习时长分数：越长越好（满分100分）
    private double calculateVideoScore(Integer duration) {
        if (duration == null) return 0;
        return Math.min(duration / 60.0, 100);
    }

    // 计划完成率分数：越大越好（满分100分）
    private double calculatePlanScore(Integer completionRate) {
        if (completionRate == null) return 0;
        return Math.min(completionRate, 100);
    }

    // 最后更新时间分数：越早越好（满分100分）
    private double calculateTimeScore(LocalDateTime updatedAt) {
        if (updatedAt == null) return 0;

        LocalDateTime now = LocalDateTime.now();
        long daysAgo = ChronoUnit.DAYS.between(updatedAt, now);

        // 0-10天：10分
        // 10-30天：30分
        // 30-60天：60分
        // 60-90天：80分
        // 90天以上：100分
        if (daysAgo <= 10) return 10;
        else if (daysAgo <= 30) return 30;
        else if (daysAgo <= 60) return 60;
        else if (daysAgo <= 90) return 80;
        else return 100;
    }
}